<template>
  <a-drawer
    :title="title"
    :closable="false"
    :maskClosable="false"
    width="70%"
    :visible="visible"
    :body-style="{ paddingBottom: '80px'}"
    @close="onClose"
  >
    <a-tabs v-model="tabIndex">
      <a-tab-pane key="1" tab="中文"></a-tab-pane>
      <a-tab-pane key="2" tab="英文"></a-tab-pane>
    </a-tabs>
    <a-form-model 
      ref="ruleForm"
      :model="form" 
      :rules="rules" 
      :label-col="labelCol" 
      :wrapper-col="wrapperCol" 
      :key="visible"
    >
      <a-form-model-item label="类型" prop="type">
        <a-select v-model="form.type" placeholder="类型">
          <a-select-option :value="1">国内公司</a-select-option>
          <a-select-option :value="2">国外公司</a-select-option>
          <a-select-option :value="3">销售商</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="公司图片" required>
        <a-upload
          list-type="picture-card"
          :file-list="fileList"
          :remove="hanldeImgRemove"
          @preview="handlePreview"
          :customRequest="handleUploadChange"
          class="upload-half"
          accept="image/*"
        >
          <div class="custom-btn" v-if="fileList.length<1">
            <a-icon type="ant-cloud" style="font-size: 26px" />
            <div class="ant-upload-text">添加图片(420*240)</div>
          </div>
        </a-upload>
      </a-form-model-item>

      <a-form-model-item label="公司名称" prop="title" v-if="tabIndex==1">
        <a-input v-model="form.title" placeholder="公司名称" />
      </a-form-model-item>
      <a-form-model-item label="英文公司名称" prop="titleEn" v-else>
        <a-input v-model="form.titleEn" placeholder="英文公司名称" />
      </a-form-model-item>
      <a-form-model-item label="公司地址" prop="address" v-if="tabIndex==1">
        <a-input v-model="form.address" placeholder="公司地址" />
      </a-form-model-item>
      <a-form-model-item label="英文公司地址" prop="addressEn" v-else>
        <a-input v-model="form.addressEn" placeholder="英文公司地址" />
      </a-form-model-item>
      <template v-if="form.type != 3">
        <a-form-model-item label="联系人" prop="linkman" v-if="tabIndex==1">
          <a-input v-model="form.linkman" placeholder="联系人" />
        </a-form-model-item>
        <a-form-model-item label="英文联系人" prop="linkmanEn" v-else>
          <a-input v-model="form.linkmanEn" placeholder="英文联系人" />
        </a-form-model-item>
      </template>
      <template v-else>
        <a-form-model-item label="销售商负责系列">
          <a-select
            v-model="form.productSeries"
            show-search
            placeholder="选择系列"
            option-filter-prop="children"
            allowClear
            style="width: 300px"
            :filter-option="(input,option) => option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0"
          >
            <a-select-option :value="item._id" v-for="item of parent.menuTree" :key="item._id">{{ item.title }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="营业时间">
          <a-input v-model="form.openTime" placeholder="营业时间" style="width: 300px" />
        </a-form-model-item>
        <a-form-model-item label="省份">
          <a-select
            show-search
            placeholder="省份"
            option-filter-prop="children"
            style="width: 300px"
            :filter-option="(input,option) => option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0"
            v-model="form.provinceId"
          >
            <a-select-option :value="item.sourceId" v-for="item in parent.provinceList" :key="item.sourceId">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="城市">
          <a-select
            show-search
            placeholder="城市"
            option-filter-prop="children"
            style="width: 300px"
            v-model="form.cityId"
            :filter-option="(input,option) => option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0"
          >
            <a-select-option :value="item.id" v-for="item of cityList" :key="item.sourceId">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-model-item>
      </template>
      <a-form-model-item label="电话号码" prop="linkmanEn">
        <a-input v-model="form.phoneNumber" placeholder="电话号码" />
      </a-form-model-item>
      <a-form-model-item label="纬度获取参考" prop="linkmanEn">
        <a href="https://api.map.baidu.com/lbsapi/getpoint/index.html" target="_blank">https://api.map.baidu.com/lbsapi/getpoint/index.html</a>
        <p><img src="~@/assets/map.jpg" alt="" style="width:100%;"></p>
      </a-form-model-item>
      <a-form-model-item label="经度">
        <a-input v-model="form.lon" placeholder="经度" />
      </a-form-model-item>
      <a-form-model-item label="纬度">
        <a-input v-model="form.lat" placeholder="纬度" />
      </a-form-model-item>
    </a-form-model>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 99999,
      }"
    >
      <a-button :style="{ marginRight: '8px' }" @click="onClose"> 关闭 </a-button>
      <a-button type="primary" @click="onSubmit"> 提交 </a-button>
    </div>
    <a-modal :visible="previewVisible" :footer="null" @cancel="previewVisible = false" width="60%">
      <img alt="example" style="width: 100%" :src="previewImage" />
    </a-modal>
  </a-drawer>
</template>

<script>
import { insertCompany,updateCompany,getCityList } from '@/api/manage'
import { showMessage } from '@/utils/mixins'
import { uploadFile,deleteFile } from '@/api/common'
import { getFileName } from '@/utils/util'

export default {
  mixins: [showMessage],
  props: {
    parent: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      tabIndex: '1',
      visible: false,
      title: '',
      labelCol: {span: 3},
      wrapperCol: {span: 21},
      form: {
        type: undefined,
        title: undefined,
        titleEn: undefined,
        address: undefined,
        addressEn: undefined,
        linkman: undefined,
        linkmanEn: undefined,
        phoneNumber: undefined,
        provinceId: undefined,
        productSeries: undefined,
        cityId: undefined,
        img: undefined,
        lon: undefined,
        lat: undefined
      },
      rules: {
        type: [{required: true, message: '公司类型不能为空', trigger: 'change'}],
        title: [{required: true, message: '公司名称不能为空', trigger: 'blur'}],
        address: [{required: true, message: '公司地址不能为空', trigger: 'blur'}],
      },
      fileList: [],
      status: undefined,
      id: undefined,
      previewVisible: false,
      previewImage: '',
      cityList: []
    }
  },
  watch: {
    'form.provinceId'(val) {
      if(val) {
        this.getCityList()
      }
    }
  },
  methods: {
    //获取城市
    getCityList() {
      getCityList({id: this.form.provinceId}).then(res => {
        console.log(res)
        if(res.code == 200) {
          this.cityList = res.data
        }
      })
    },
    // 预览图片
    handlePreview(file) {
      this.previewVisible = true
      this.previewImage = file.url
    },
    // 删除图片
    hanldeImgRemove(file) {
      deleteFile({id: file.uid}).then(res => {
        if(res.code == 200) {
          this.showMessage(res,() => {
            this.fileList = []
          })
        }
      })
    },
    //自定义上传图片
    handleUploadChange(file) {
      const _this = this
      file = file.file
      const formdata = new FormData()
      formdata.append('file', file)
      uploadFile(formdata).then(res => {
        if(res.code == 200) {
          file.url = res.data.link
          file.id = res.data._id
          file.uid = res.data._id
          file.fileame = getFileName( res.data.link )
          _this.fileList.push(file)
          _this.form.img = res.data._id
        }else {
          _this.$message.error(res.message)
        }
      })
    },
    // 提交
    onSubmit () {
      this.$refs.ruleForm.validate(vaild => {
        if(vaild) {
          if(!this.form.img) {
            this.$message.warning('公司图片不能为空')
            return
          }
          if(this.status == 1) {
            insertCompany(this.form).then(res => {
              this.showMessage(res, () => {
                this.onClose()
                this.parent.getList()
              })
            })
          }else {
            updateCompany({
              id: this.id,
              ...this.form
            }).then(res => {
              this.showMessage(res, () => {
                this.onClose()
                this.parent.getList()
              })
            })
          }
        }else {
          return false
        }
      })
    },
    // 关闭
    onClose() {
      for(const key in this.form) {
        this.form[key] = undefined
      }
      this.status = undefined
      this.visible = false
      this.id = undefined
      this.fileList = []
      this.tabIndex = '1'
    }
  }
}
</script>